<?php

namespace app\shop\model\employ;

use app\common\model\employ\Poster as PosterModel;
use app\proxy\model\proxy\Region;
use app\common\model\region\Region as Region1;
use app\shop\model\supplier\Supplier as SupplierModel;

class Poster extends PosterModel
{
    public function getList($params)
    {
        $model = $this;
        if (isset($params['keywords']) && $params['keywords']) {
            $model = $model->where('name', 'like', '%' . $params['keywords'] . '%');
        }
        if (isset($params['is_audit'])) {
            $model = $model->where('is_audit', $params['is_audit']);
        }

        if (isset($params['province_id']) && $params['province_id']) {
            $model = $model->where('province_id', '=', $params['province_id']);

        }
        if (isset($params['city_id']) && $params['city_id']) {
            $model = $model->where('city_id', '=', $params['city_id']);
        }
        if (isset($params['region_id']) && $params['region_id']) {
            $model = $model->where('region_id', '=', $params['region_id']);
        }


        $map = [];
        if (isset($params['search']) && $params['search']) {
            $map[] = ['name', 'like', '%' . trim($params['search']) . '%'];
        }

        $map2 = [];
        if($map){
            $shop_supplier_ids = (new SupplierModel())->where($map) -> column('shop_supplier_id');
            $map2[] = ['shop_supplier_id','in',$shop_supplier_ids];
        }
        // 查询列表数据
        return $model->with(["qr_code", 'bg_image', 'supplier'])->where('is_delete', '=', '0')
            ->withAttr('proxy_name', function ($value, $data) {
                return Region::getProxyName($data, 0);
            })->withAttr('city_name', function ($value, $data) {
                return Region1::getMergerName($data["city_id"]);
            })
            ->where($map2)
            ->append(['proxy_name', 'city_name'])
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
}